import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;


public class ParserMain {
	
	static WebClient pagina;
	static String searchPage;
	static Object content;
	
	boolean hasCommon = true;
	String commonName ;
	String latinName ;
	String plantType;
	String description;
	String origin;
	boolean flower;
	String season;
	String easeGrow;
	String temperature;
	String humidity;
	String light;
	String soil;
	String water;
	String fertilizer;

	public ParserMain(String search, String type){
        pagina = null;
        searchPage = search;
        commonName = null;
        latinName = null;
        plantType = null;
        description = null;
        origin = null;
        flower = false;
        season = null;
        easeGrow = null;
        temperature = null;
        humidity = null;
        light = null;
        soil = null;
        water = null;
        fertilizer = null;
	}
	
	public void appendToFile(){
			
		 String newline = System.getProperty("line.separator");
		 try {
		        BufferedWriter out = new BufferedWriter(new FileWriter("plantDB.txt", true));
		        out.write("************************************" + newline);
		        out.write(commonName + newline);
		        out.write(latinName + newline);
		        out.write( plantType + newline);
		        out.write( description + newline);
		        out.write(origin + newline);
		        out.write(flower + newline);
		        out.write(season + newline);
		        out.write( easeGrow + newline);
		        out.write( temperature + newline);
		        out.write(humidity + newline);
		        out.write( light + newline);
		        out.write(soil + newline);
		        out.write( water + newline);
		        out.write( fertilizer + newline);
		        out.write( searchPage + newline );
		        out.write( newline);
		        out.close();
		    } catch (IOException e) {
		    }
	}
	
	public void process(String page){
		int position1 = 0;
		position1 = page.indexOf("mainHolder_commonNames") + "mainHolder_commonNames".length() +2;
		
		if( position1 < 0 ){
			System.out.println("Position1"); return; }
		int position2 = 0;
		position2 = page.indexOf("</span>", position1);
		if( position2 < 0 ){
			System.out.println("Position2"); return; }
	    String first_name = page.substring(position1,position2);
	    if( first_name.contains("/strong") ){
	    	hasCommon = false;
	    	int pos = first_name.indexOf("/strong") + "/strong>".length();
	    	latinName = first_name.substring(pos, first_name.length());
	    } else {
	    	hasCommon = true;
	    	commonName = first_name;
	    }

	    // serching for latin name
	    if( hasCommon == true){
		    page = page.substring(position1, page.length());
		    position1 = page.indexOf("mainHolder_latinNames") + "mainHolder_latinNames".length() + 2;
		    position2 = page.indexOf("</span>", position1);
		    if( position1 + 3 < position2 ){
		    	position1 = page.indexOf("</strong>", position1)+ "</strong>".length();
		    	latinName =  page.substring(position1, position2); }
	   }
	    
	    page = page.substring(position1, page.length());
	    
	    // Family Name
	    position1 = page.indexOf("mainHolder_familyNames") + "mainHolder_familyNames".length() + 2;
	    position1 = page.indexOf("'>", position1) + 2;
	    position2 = page.indexOf("/a",position1) - 1;
	    plantType = page.substring(position1, position2);
	    
	    // Description
	    position1 = page.indexOf("mainHolder_description") + "mainHolder_description".length() + 2;
	    position1 = page.indexOf("mainHolder_description",position1) + "mainHolder_description".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    description = page.substring(position1, position2);
	    
	    //Origin
	    position1 = page.indexOf("mainHolder_origin") + "mainHolder_origin".length() + 2;
	    position1 = page.indexOf("mainHolder_origin",position1) + "mainHolder_origin".length() + 3;
	    position2 = page.indexOf("</span>",position1);
	    origin = page.substring(position1, position2);
	    
	    
	    String man ;
	    //Flowering
	    position1 = page.indexOf("mainHolder_flowering") + "mainHolder_flowering".length() + 2;
	    position1 = page.indexOf("mainHolder_flowering",position1) + "mainHolder_flowering".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    man = page.substring(position1, position2);
	    if( man.contains("Yes") )
	    	flower = true;
	    else
	    	flower = false;
	    
	    //Season
	    position1 = page.indexOf("_mainHolder_season") + "_mainHolder_season".length() + 2;
	    position1 = page.indexOf("_mainHolder_season",position1) + "_mainHolder_season".length() + 3;
	    position2 = page.indexOf("</span>",position1);
	    season = page.substring(position1, position2);
	    
	    // Easy to Grow
	    position1 = page.indexOf("mainHolder_easyToGrow") + "mainHolder_easyToGrow".length() + 2;
	    position1 = page.indexOf("mainHolder_easyToGrow",position1) + "mainHolder_easyToGrow".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    easeGrow = page.substring(position1, position2);
	    
	    // Temperature
	    position1 = page.indexOf("mainHolder_temperature") + "mainHolder_temperature".length() + 2;
	    position1 = page.indexOf("mainHolder_temperature",position1) + "mainHolder_temperature".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    temperature = page.substring(position1, position2);
	    
	    //Humidity 
	    position1 = page.indexOf("mainHolder_humidity") + "mainHolder_humidity".length() + 2;
	    position1 = page.indexOf("mainHolder_humidity",position1) + "mainHolder_humidity".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    humidity = page.substring(position1, position2);
	    
	    // Light Source
	    position1 = page.indexOf("mainHolder_lightSource") + "mainHolder_lightSource".length() + 2;
	    position1 = page.indexOf("mainHolder_lightSource",position1) + "mainHolder_lightSource".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    man = page.substring(position1, position2);
	   
	    position1 = page.indexOf("mainHolder_lightDirection",position1) + "mainHolder_lightDirection".length() + 2;
	    position2 = page.indexOf("</span>",position1);
	    light = man + page.substring(position1, position2);
	    
	    //Soil mainHolder_soilIngredients
	    position1 = page.indexOf("mainHolder_soilIngredients",position1) + "mainHolder_soilIngredients".length() + 3;
	    position2 = page.indexOf("</span>",position1);
	    soil = page.substring(position1, position2);
	    
	    // Water 
	    position1 = page.indexOf("mainHolder_water") + "mainHolder_water".length() + 2;
	    position1 = page.indexOf("mainHolder_water",position1) + "mainHolder_water".length() + 3;
	    position2 = page.indexOf("</span>",position1);
	    water = page.substring(position1, position2);
	    
	    // Fertilizer
	    position1 = page.indexOf("mainHolder_fertilizer") + "mainHolder_fertilizer".length() + 2;
	    position1 = page.indexOf("mainHolder_fertilizer",position1) + "mainHolder_fertilizer".length() + 3;
	    position2 = page.indexOf("</span>",position1);
	    fertilizer = page.substring(position1, position2);
	    
	}
	
	public void doIt(){
		String mime = new String();
        try{

            try{
               WebClient pagina = new WebClient(searchPage.trim());
               content = pagina.getContent();
               mime = pagina.getMIMEType();
              System.out.println(mime);
              process(content.toString());
              appendToFile();

           } catch (MalformedURLException e){
               pagina = null;
               System.out.println("Nu am putut lua pagina ");}

       }catch (IOException e){
            pagina = null;
            System.out.println("Problema la IO "+e);
       }
		
	}
	
}
